﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using zhangmenren.IService;
using zhangmenren.ViewModel;

namespace zhangmenren.Areas.Admin.Controllers
{
	[Area(areaName: "Admin")]
	public class ApplyController : ABaseController
    {
		private IApplyService applyService;
        private IUserService userService;
        public ApplyController(IUserService  _userService ,IApplyService _applyService)
		{
			applyService = _applyService;
            userService = _userService;

        }
		/// <summary>
		/// 后台申请掌门人
		/// </summary>
		/// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }


		/// <summary>
		/// 查询申请列表
		/// </summary>
		/// <param name="page"></param>
		/// <returns></returns>
		public async Task<string> GetApply(int page = 1)
		{
			int total = 0;
			IEnumerable<ApplyModel> dataList = null;
			dataList = await applyService.Select();
			if (dataList.Any())
			{
				total = dataList.Count();
			}
			return JsonConvert.SerializeObject(new { code = "0", msg = "查询成功", data = dataList, count = total });

		}

		/// <summary>
		/// 更新审核状态
		/// </summary>
		/// <param name="id"></param>
		/// <param name="state"></param>
		/// <returns></returns>
		[HttpPost]
		public async Task<JsonResult> UpState( int id,int userid,int state=1)
		{
			var result = await applyService.UpState(id, state);

            var set = await userService.SetZMRID(Convert.ToInt32(Untils.RandomHelper.GenerateRandomCode(8)),id);

			return Json(new { state = "ok", msg = "修改成功" });
		}

        [HttpPost]
        public JsonResult Del(int id = 0)
        {
            if (id != 0)
            {
                var result = applyService.Del(id);
                return Json(new { state = "ok", msg = "删除成功" });
            }
            return Json(new { state = "fail", msg = "标识错误" });
        }

    }
}